package com.ywl.exercise.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class SinaWeatherAsker{
    
    public String getWeather() {
        /* 从新浪天气接口中获取近3日天气，温度，穿衣建议 */
        String weatherUrl;
        String city = "杭州";
        XmlUtil xmlUtil = XmlUtil.getInstance();
        String encodeCity = null;
        try {
            encodeCity = URLEncoder.encode(city, "gb2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //StringBuffer weather = new StringBuffer(city + "天气：\n");
        StringBuffer weather = new StringBuffer();
        for (int day = 0; day<3; day++) {
			/*从url返回的xml文档中获取天气信息*/
            weatherUrl = "http://php.weather.sina.com.cn/xml.php?city=" + encodeCity
                    + "&password=DJOYnieT8234jlsK&day=" + day;
            xmlUtil.setURL(weatherUrl);
            if (day == 0)
                weather.append("今天：");
            if (day == 1)
                weather.append("明天：");
            if (day == 2)
                weather.append("后天：");
            weather.append(xmlUtil.getContentByNodeName("status1"));
            weather.append("~");
            weather.append(xmlUtil.getContentByNodeName("status2"));
            weather.append(xmlUtil.getContentByNodeName("temperature2"));
            weather.append("℃~");
            weather.append(xmlUtil.getContentByNodeName("temperature1"));
            weather.append("℃");
            weather.append("\n");
        }
        return weather.toString();
    }
    
    public static void main(String[] args) {
    	SinaWeatherAsker swa = new SinaWeatherAsker();
    	System.out.println(swa.getWeather());
	}
}
